package com.fc.v2.controller.api;


import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.model.MapObject;
import com.fc.v2.service.TSysDatasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Api(value = "文件上传API")
@Controller
@RequestMapping("/api/file")
public class AFileController {

    @Autowired
    private TSysDatasService sysDatasService;

    /**
     * 文件上传,用于上传封面图片
     * @param file
     * @return
     * @author fuce
     * @Date 2021年2月25日
     */
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("file") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                //插入文件存储表
                Map<String,String> map=sysDatasService.insertSelective(file,true);
                if(map!=null){
                    return AjaxResult.successData(200, map);
                }
            }
            return AjaxResult.error();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * editor文件上传
     * @param file
     * @return
     * @author fuce
     * @Date 2021年2月25日
     */
    @ApiOperation(value = "editor文件上传", notes = "editor文件上传")
    @PostMapping("/editor/upload")
    @ResponseBody
    public AjaxResult editorUpload(@RequestParam("file") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                //插入文件存储表
                Map<String,String> map=sysDatasService.insertSelective(file,false);
                if(map!=null){
                    String imgName = map.get("imgName");
                    String src = "/static/img/"+imgName;
                    return AjaxResult.successData(0, new MapObject().put("src",src).put("title",imgName));
                }
            }
            return AjaxResult.error();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }


}
